package com.hezuti.common.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CreateImageUtil {
	private static char[] arr;
	static{
		String str="我是中国人中南西北东123456789";
		for(int i='A';i<'Z';i++){
			str+=String.valueOf((char)i);
		}
		for(int i='a';i<'z';i++){
			str+=String.valueOf((char)i);
		}
		arr=str.toCharArray();
	}
	public static Map<String,InputStream> getImage() throws ImageFormatException, IOException{
		BufferedImage bi=new BufferedImage(120, 50, BufferedImage.TYPE_3BYTE_BGR);
		Graphics g = bi.getGraphics();
		Random rad=new Random();
		g.setColor(new Color(rad.nextInt(6)+250,rad.nextInt(56)+190,rad.nextInt(125)+100));
		g.fillRect(0, 0, 200, 200);
		g.setColor(new Color(rad.nextInt(256),rad.nextInt(256),rad.nextInt(256)));
		g.drawArc(40, 40, 100, 100, 10, 360);
		for(int i=0;i<50;i++){
			g.setColor(new Color(rad.nextInt(104)+152,rad.nextInt(104)+152,rad.nextInt(104)+152));
			g.drawLine(rad.nextInt(100), rad.nextInt(50), rad.nextInt(100), rad.nextInt(50));
		}
		
		String str="";
		String s="";
		for(int i=0;i<5;i++){
			g.setFont(new Font(null,Font.BOLD|Font.ITALIC|Font.HANGING_BASELINE,rad.nextInt(10)+20));
			g.setColor(new Color(rad.nextInt(152),rad.nextInt(152),rad.nextInt(152)));
			g.drawString(s=String.valueOf(arr[rad.nextInt(arr.length)]),20*i+5,30);
			str+=s;
		}
		ByteArrayOutputStream bas=new ByteArrayOutputStream();
		JPEGImageEncoder encode = JPEGCodec.createJPEGEncoder(bas);
			encode.encode(bi);
			byte[] b=bas.toByteArray();
			InputStream image=new ByteArrayInputStream(b);
			Map<String, InputStream> map = new HashMap<String,InputStream>();
			map.put(str, image);
			return map;
	}
}
